﻿var currStyle;

chrome.extension.sendRequest("initialize", function (response) {
    currStyle = response.mode;
    
    var styleNode = document.createElement("link");
    styleNode.type = "text/css";
    styleNode.rel = "stylesheet";
    styleNode.href = chrome.extension.getURL("sublime_" + currStyle + ".css");
    styleNode.id = currStyle + "_mode";
    document.documentElement.appendChild(styleNode);

});


function switchStyle(newStyle) {
    var styleNode = document.createElement("link");
    styleNode.type = "text/css";
    styleNode.rel = "stylesheet";
    styleNode.href = chrome.extension.getURL("sublime_" +  newStyle + ".css");
    styleNode.id =  newStyle + "_mode";
    document.documentElement.appendChild(styleNode);
    
    document.documentElement.removeChild(document.getElementById(currStyle + "_mode"));
    currStyle =  newStyle;
    
    setTimeout(function () {
        var b = document.getElementById('sub-tree');
        b.style.height = document.documentElement.clientHeight - b.getBoundingClientRect().top + "px";
        var c = document.getElementById('entries');
        c.style.height = document.documentElement.clientHeight - document.getElementById('viewer-footer').getBoundingClientRect().height - c.getBoundingClientRect().top + "px";
    }
    , 500);
    
}


function switchdiv(e){
   if (searchdiv.className){
      searchdiv.className = ""; //display
      var hidesearchdiv = function(){
         document.body.removeEventListener('click',hidesearchdiv,false);
         searchdiv.className = "hidden";
      };
      document.body.addEventListener('click',hidesearchdiv,false);
   }
   else
      searchdiv.className = "hidden"; //hide
   //searchdiv.className = (searchdiv.className)?"":"hidden";
   e.stopPropagation();

}
var searchdiv;
var addSearch = function () {
    //if (document.readyState == "complete") {
    var sicon = document.createElement('div');
    sicon.setAttribute('id', "searchicon");
    sicon.innerHTML = "SEARCH";
    document.body.appendChild(sicon);
    searchdiv = document.getElementById("search");
    searchdiv.setAttribute("class", "hidden");
    searchdiv.addEventListener('click', function (e) {
        e.stopPropagation();
    }
    , false);
    sicon.onclick = switchdiv;
    //}
    
    setTimeout(function () {
        var b = document.getElementById('sub-tree');
        b.style.height = document.documentElement.clientHeight - b.getBoundingClientRect().top + "px";
        var c = document.getElementById('entries');
        c.style.height = document.documentElement.clientHeight - document.getElementById('viewer-footer').getBoundingClientRect().height - c.getBoundingClientRect().top + "px";
    }
    , 500);

};

      
document.addEventListener("DOMContentLoaded", addSearch, false);

